import java.awt
import javax.swing
from javax.swing import JSplitPane, JScrollPane
import random
import math as Math
import MatrixIO
from Matrix import Matrix,size

class PyColorMap(java.lang.Object):
	COLOUR_MAX_VALUE = 255;
	maxAbsValue=None;
	minVal=None;
	tabColor = [0,60,102,153,204,COLOUR_MAX_VALUE];
	TAB_LIMIT_VALUE = [25,76,123,179,230,COLOUR_MAX_VALUE];

	def getColor(self,value):
		if (self.maxAbsValue == None):
			self.maxAbsValue = 2;
		dValeur = value;
		colorIndex = (self.COLOUR_MAX_VALUE * Math.fabs(dValeur) / self.maxAbsValue);
		if (colorIndex > self.COLOUR_MAX_VALUE):
			colorIndex = self.COLOUR_MAX_VALUE;
		for i in xrange(0,len(self.TAB_LIMIT_VALUE)):
			if (colorIndex <= self.TAB_LIMIT_VALUE[i]):
				colorIndex = self.tabColor[i];
				break;
		if (dValeur >= 0):
			return java.awt.Color(0, colorIndex, 0);
		return java.awt.Color(colorIndex, 0, 0);



class Clustergram(javax.swing.JComponent):
	tree = None
	heatmap = None
	
	def paintComponent(self,g):
		pass
	
#######
class PyAct(java.awt.event.ActionListener):
	actionPerformed = None;
	def __init__(self,p,f):
		self.actionPerformed = f;
		self.p = p;

class PyHeatMap(javax.swing.JLabel):
	PixelSize = 10;
	CMap = PyColorMap();
	XINIT = 30
	zdim = None
	f = None
	act = None
	def actionPerformed(self,e):
		x = e.getActionCommand()
		width=0
		height=0
		if (x == 'plus'):
			width  = (self.zdim.getWidth()  * 1.1);
			height = (self.zdim.getHeight() * 1.1);
		else:
			width  = (self.zdim.getWidth()*0.1)
			height = (self.zdim.getHeight()*0.1)
		self.zdim.setSize(width,height);
		if (self.zdim.getWidth() == 0) and self.zdim.getHeight()==0:
			self.zdim.setSize(1.0,1.0);
		self.setPreferredSize(self.zdim);
		self.repaint();
	def __init__(self):
		[self.E,self.Elabels,self.Explabels] = MatrixIO.readmatrix('Etest.txt');
		self.zdim = java.awt.Dimension();
		self.zdim.setSize(1.0,1.0)
		self.f = lambda x: self.actionPerformed(x)
		self.act = PyAct(self,self.f)
	def paintComponent(self,g):
		x=self.XINIT;y=0;
		print self.zdim		
		g.scale(self.zdim.getHeight(),self.zdim.getWidth())  # magnify
		[genes,exps]=size(self.E)
		for i in xrange(1,genes):
			g.setColor(java.awt.Color.black)
			g.drawString(self.Elabels[i], 0, y) 
			for j in xrange(1,exps):
				mycolor = self.CMap.getColor(self.E[i,j])
				g.setColor(mycolor)
				g.fillRect(x, y, 10, 10);
				x=x+10
			y=y+11
			x=self.XINIT

class GoTreeMap(javax.swing.JComponent):
	tree = None
	Categories = None
	GeneCategoryMap = {}
	
	def __init__(self):
		pass


# need to figure out what tree typSe is
# create a clustermap from gene expression clusters, show tree in left hand side.





# testcase
jf = javax.swing.JFrame()
map = JScrollPane(PyHeatMap(),JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS)
one = javax.swing.JButton("plus");
two = javax.swing.JButton("minus");

d = JSplitPane(JSplitPane.HORIZONTAL_SPLIT,JSplitPane(JSplitPane.HORIZONTAL_SPLIT,one,two),map);
jf.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
one.addActionListener(map.viewport.getView().act);
two.addActionListener(map.viewport.getView().act);
jf.add(d)
jf.setSize(400,400)
jf.show()
# 10 pixels per rectangle?
 